package com.elitel.frame.core.util;

import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ImportExcel {

    //正则表达式 用于匹配属性的第一个字母
    private static final String REGEX = "[a-zA-Z]";

    /**
     * 功能: Excel数据导入到数据库
     * 参数: originUrl[Excel表的所在路径]
     * 参数: startRow[从第几行开始]
     * 参数: endRow[到第几行结束
     *                  (0表示所有行;
     *                  正数表示到第几行结束;
     *                  负数表示到倒数第几行结束)]
     * 参数: clazz[要返回的对象集合的类型]
     */
    public static List<?> importExcel(File file,int startRow,int endRow,Class<?> clazz) throws IOException {
        //是否打印提示信息
        boolean showInfo=false;
        return doImportExcel(file,startRow,endRow,showInfo,clazz);
    }
    
    public static List<?> doImportExcelByInputstream(InputStream fis, String filename, int startRow,int endRow,boolean showInfo,Class<?> clazz) throws IOException {
    	
    	String ext = filename.substring(filename.lastIndexOf("."));
        Workbook wb = null;
//        FileInputStream fis=null;
        List<Row> rowList = new ArrayList<Row>();
        try {
//            fis = new FileInputStream(file);
            // 去读Excel
            if(".xls".equals(ext)){  
                wb = new HSSFWorkbook(fis);  
            }else if(".xlsx".equals(ext)){  
                wb = new XSSFWorkbook(fis);  
            }else{  
                wb=null;  
            }  
//            wb = new HSSFWorkbook(fis);
            Sheet sheet = wb.getSheetAt(0);
            // 获取最后行号
            int lastRowNum = sheet.getLastRowNum();
            if (lastRowNum > 0) { // 如果>0，表示有数据
                out("\n开始读取名为【" + sheet.getSheetName() + "】的内容：",showInfo);
            }
            Row row = null;
            // 循环读取
            for (int i = startRow; i <= lastRowNum + endRow; i++) {
                row = sheet.getRow(i);
                if (row != null) {
                    rowList.add(row);
                    out("第" + (i + 1) + "行：",showInfo,false);
                    // 获取每一单元格的值
                    for (int j = 0; j < row.getLastCellNum(); j++) {
                        String value = getCellValue(row.getCell(j));
                        if (!value.equals("")) {
                            out(value + " | ",showInfo,false);
                        }
                    }
                    out("",showInfo);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            fis.close();
        }
        if(clazz == null) {
            return returnMapList(rowList,clazz);
        }
        return returnObjectList(rowList,clazz);
    }
    /**
     * 功能:真正实现导入
     */
    private static List<Object> doImportExcel(File file,int startRow,int endRow,boolean showInfo,Class<?> clazz) throws IOException {
        // 判断文件是否存在
//        File file = new File(originUrl);
//        String ext = originUrl.substring(originUrl.lastIndexOf("."));  
    	String lname = file.getName();
    	String ext = lname.substring(lname.lastIndexOf("."));
        if (!file.exists()) {
            throw new IOException("文件名为" + file.getName() + "Excel文件不存在！");
        }
//        HSSFWorkbook wb = null;
        Workbook wb = null;
        FileInputStream fis=null;
        List<Row> rowList = new ArrayList<Row>();
        try {
            fis = new FileInputStream(file);
            // 去读Excel
            if(".xls".equals(ext)){  
                wb = new HSSFWorkbook(fis);  
            }else if(".xlsx".equals(ext)){  
                wb = new XSSFWorkbook(fis);  
            }else{  
                wb=null;  
            }  
//            wb = new HSSFWorkbook(fis);
            Sheet sheet = wb.getSheetAt(0);
            // 获取最后行号
            int lastRowNum = sheet.getLastRowNum();
            if (lastRowNum > 0) { // 如果>0，表示有数据
                out("\n开始读取名为【" + sheet.getSheetName() + "】的内容：",showInfo);
            }
            Row row = null;
            // 循环读取
            for (int i = startRow; i <= lastRowNum + endRow; i++) {
                row = sheet.getRow(i);
                if (row != null) {
                    rowList.add(row);
                    out("第" + (i + 1) + "行：",showInfo,false);
                    // 获取每一单元格的值
                    for (int j = 0; j < row.getLastCellNum(); j++) {
                        String value = getCellValue(row.getCell(j));
                        if (!value.equals("")) {
                            out(value + " | ",showInfo,false);
                        }
                    }
                    out("",showInfo);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            
        }

        return returnObjectList(rowList,clazz);
    }

    /**
     * 功能:获取单元格的值
     */
    private static String getCellValue(Cell cell) {
        Object result = "";
        if (cell != null) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                result = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
            	 if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式  
                     SimpleDateFormat sdf = null;  
                     if (cell.getCellStyle().getDataFormat() == HSSFDataFormat  
                             .getBuiltinFormat("h:mm")) {  
                         sdf = new SimpleDateFormat("HH:mm");  
                     } else {// 日期  
                         sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
                     }  
                     Date date = cell.getDateCellValue();  
                     result = sdf.format(date);  
                 } else if (cell.getCellStyle().getDataFormat() == 58) {  
                     // 处理自定义日期格式：m月d日(通过判断单元格的格式id解决，id的值是58)  
                     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
                     double value = cell.getNumericCellValue();  
                     Date date = org.apache.poi.ss.usermodel.DateUtil  
                             .getJavaDate(value);  
                     result = sdf.format(date);  
                 } else {  
                     double value = cell.getNumericCellValue();  
                     CellStyle style = cell.getCellStyle();
                     DecimalFormat format = new DecimalFormat();  
                     String temp = style.getDataFormatString();  
                     // 单元格设置成常规  
                     if (temp.equals("General")) {  
                         format.applyPattern("#");  
                     }  
                     result = format.format(value);  
                 }  
//                result = cell.getNumericCellValue();
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                result = cell.getBooleanCellValue();
                break;
            case Cell.CELL_TYPE_FORMULA:
                result = cell.getCellFormula();
                break;
            case Cell.CELL_TYPE_ERROR:
                result = cell.getErrorCellValue();
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            default:
                break;
            }
        }
        return result.toString();
    }

    /**
     * 功能:返回指定的对象集合
     */
    private static List<Object> returnObjectList(List<Row> rowList,Class<?> clazz) {
        List<Object> objectList=null;
        Object obj=null;
        String attribute=null;
        String value=null;
        int j=0;
        try {   
            objectList=new ArrayList<Object>();
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Row row : rowList) {
                j=0;
                obj = clazz.newInstance();
                for (Field field : declaredFields) {    
                    attribute=field.getName().toString();
                    value = getCellValue(row.getCell(j));
                    setAttrributeValue(obj,attribute,value);    
                    j++;
                }
                objectList.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objectList;
    }

    /**
     * 功能:返回指定的对象集合(以Map的形式)
     */
    private static List<Object> returnMapList(List<Row> rowList,Class<?> clazz) {
        List<Object> objectList = new ArrayList<Object>();
        String[] headers = new String[rowList.get(0).getLastCellNum()];
        for (int j = 0; j < rowList.get(0).getLastCellNum(); j++) {
            headers[j] = getCellValue(rowList.get(0).getCell(j));
        }
        for (int i = 1; i < rowList.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            for (int j = 0; j < rowList.get(i).getLastCellNum(); j++) {
                String paramvalue = getCellValue(rowList.get(i).getCell(j));
                map.put(headers[j], paramvalue);
            }
            objectList.add(map);
        }

        return objectList;
    }
    /**
     * 功能:给指定对象的指定属性赋值
     */
    private static void setAttrributeValue(Object obj,String attribute,String value) {
        //得到该属性的set方法名
        String method_name = convertToMethodName(attribute,obj.getClass(),true);
        Method[] methods = obj.getClass().getMethods();
        for (Method method : methods) {
            /**
             * 因为这里只是调用bean中属性的set方法，属性名称不能重复
             * 所以set方法也不会重复，所以就直接用方法名称去锁定一个方法
             * （注：在java中，锁定一个方法的条件是方法名及参数）
            */
            if(method.getName().equals(method_name))
            {
                Class<?>[] parameterC = method.getParameterTypes();
                try {
                    /**如果是(整型,浮点型,布尔型,字节型,时间类型),
                     * 按照各自的规则把value值转换成各自的类型
                     * 否则一律按类型强制转换(比如:String类型)
                    */
                	if(value != null && !"".equals(value)) {
                    if(parameterC[0] == int.class || parameterC[0]==Integer.class)
                    {	
                    	if(value.indexOf(".")!=-1){
                    		value = value.substring(0, value.lastIndexOf("."));
                		}
                        method.invoke(obj,Integer.valueOf(value));
                        break;
                    }else if(parameterC[0] == float.class || parameterC[0]==Float.class){
                        method.invoke(obj, Float.valueOf(value));
                        break;
                    }else if(parameterC[0] == double.class || parameterC[0]==Double.class)
                    {
                        method.invoke(obj, Double.valueOf(value));
                        break;
                    }else if(parameterC[0] == byte.class || parameterC[0]==Byte.class)
                    {
                        method.invoke(obj, Byte.valueOf(value));
                        break;
                    }else if(parameterC[0] == boolean.class|| parameterC[0]==Boolean.class)
                    {
                        method.invoke(obj, Boolean.valueOf(value));
                        break;
                    }else if(parameterC[0] == Date.class)
                    {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date=null;
                        try {
                            date=sdf.parse(value);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        method.invoke(obj,date);
                        break;
                    }else
                    {
                        method.invoke(obj,parameterC[0].cast(value));
                        break;
                    }
                }/*else {
                	method.invoke(obj, "");
                }*/
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 功能:根据属性生成对应的set/get方法
     */
    private static String convertToMethodName(String attribute,Class<?> objClass,boolean isSet) {
        /** 通过正则表达式来匹配第一个字符 **/
        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(attribute);
        StringBuilder sb = new StringBuilder();
        /** 如果是set方法名称 **/
        if(isSet)
        {
            sb.append("set");
        }else{
        /** get方法名称 **/
            try {
                Field attributeField = objClass.getDeclaredField(attribute);
                /** 如果类型为boolean **/
                if(attributeField.getType() == boolean.class||attributeField.getType() == Boolean.class)
                {
                    sb.append("is");
                }else
                {
                    sb.append("get");
                }
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        /** 针对以下划线开头的属性 **/
        if(attribute.charAt(0)!='_' && m.find())
        {
            sb.append(m.replaceFirst(m.group().toUpperCase()));
        }else{
            sb.append(attribute);
        }
        return sb.toString();
    }

    /**
     * 功能:输出提示信息(普通信息打印)
     */
    private static void out(String info, boolean showInfo) {
        if (showInfo) {
            System.out.print(info + (showInfo ? "\n" : ""));
        }
    }

    /**
     * 功能:输出提示信息(同一行的不同单元格信息打印)
     */
    private static void out(String info, boolean showInfo, boolean nextLine) {
        if (showInfo) {
            if(nextLine)
            {
                System.out.print(info + (showInfo ? "\n" : ""));
            }else
            {
                System.out.print( info );
            }
        }
    }

}
